const express = require('express');
const router = express.Router()
const pool = require('../utils/pool')

//添加门店

router.post('/addmd',(req,res,next)=>{
    var obj = req.body
    let sql = 'insert into shop set ?'
    pool.query(sql,[obj],(err,r)=>{
        if(err) {
            return next(err)
        }
		console.log(r)
		// 插入数据不需要判断
		res.send({code: 200, msg: '添加成功'})
    })
})

//添加店长账户
router.post('/addmddz',(req,res,next)=>{
    let obj = req.body
    let mpwd = obj.mpwd
    delete obj.mpwd
    let {ident,smid,muser,mname} = req.body
    delete obj.mname
    let ip = ident+'-'+smid
    obj.access = 1
    //验证用户是否存在
    pool.query('select * from merchant where muser=?',[obj.muser],(err,r)=>{
        if(r.length){
            res.send({code:406,msg:'账户已存在'})
            return
        }else{
            let sql = 'insert into merchant set mpwd=MD5(?),account=(select meid from merchant_info where me_name=?), ?;update merchant set ident=? where mid=(select mid from merchant where muser=?);update shop set shop_ident=? where sid=?;'
        
            pool.query(sql,[mpwd,mname,obj,ip,muser,ip,smid],(err,r)=>{
                if(err){
                    return next(err)
                }
                res.send({code: 200, msg: '添加成功'})
            })
        }
    })
})



module.exports = router;
